﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wedn.BLL.Common;

namespace Wedn.Web.UI
{
    public partial class index : System.Web.UI.Page
    {
        /// <summary>
        /// 当前页面开始载入时间(毫秒)
        /// </summary>
        public static DateTime starttick;
        /// <summary>
        /// 主题名称(文件夹)
        /// </summary>
        public string themeName = "default";

        /// <summary>
        /// 模板路径
        /// </summary>
        private string templatePath = null;

        /// <summary>
        /// 页面类型
        /// </summary>
        public string pageType = RequestHelper.QueryString("type", true);

        protected void Page_PreInit(object sender, EventArgs e)
        {
            starttick = DateTime.Now;
            string path = string.Format("/themes/{0}/site.master", themeName);
            //加载母版页
            if (System.IO.File.Exists(Server.MapPath(path)))
            {
                Page.MasterPageFile = path;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            MasterPage master = (MasterPage)this.Master;
            ContentPlaceHolder content;
            if (master != null)
            {
                content = (ContentPlaceHolder)master.FindControl("main");
            }
            else
            {
                Response.Write("<script>\nwindow.alert('该模版发生问题, 请检查模版结构是否正确!\\nPS: 模版主目录下应有site.master文件')\n</script>");
                return;
            }
            //根据页面类型加载页面模版
            switch (pageType)
            {
                case "index":
                    //需要虚拟路径,不能使用物理路径
                    //templatePath = Server.MapPath(string.Format("{0}themes/{1}/templates/uc_index.ascx", ConfigHelper.SitePath, themeName));
                    templatePath = string.Format("{0}themes/{1}/templates/t_index.htm", ConfigHelper.SitePath, themeName);
                    break;
                case "post":
                    templatePath = string.Format("{0}themes/{1}/templates/t_index", ConfigHelper.SitePath, themeName);
                    break;
                default:
                    templatePath = string.Format("{0}themes/{1}/templates/t_index", ConfigHelper.SitePath, themeName);
                    break;
            }
            //判断是否存在模版文件, 支持两种后缀名(可拓展)
            if (System.IO.File.Exists(Server.MapPath(templatePath + ".ascx")))
            {
                Control control = LoadControl(templatePath + ".ascx");
                content.Controls.Add(control);
            }
            else if (System.IO.File.Exists(Server.MapPath(templatePath + ".htm")))
            {
                Control control = LoadControl(templatePath + ".htm");
                content.Controls.Add(control);
            }
            //Control index = Page.LoadControl(templatePath);
            //index.ID = pageType;
            ////((Wedn.WebApp.themes.child_free.templates.uc_index)index).title = "ssss";
            ////((ITest)index).title = "测试";
            //content.Controls.Add(index);
        }
    }
}